package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.MysqlUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * CompanyServiceImpl
 * 2021/10/17  17:17
 * created by linjinyang
 */
public class CompanyServiceImpl implements CompanyService {
    /*
    保存
     */
    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession对象
            sqlSession = MysqlUtils.getSqlSession();
            //获取动态代理对象
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            //获取uuid
            String id = UUID.randomUUID().toString();
            //将id添加入company对象中
            company.setId(id);
            mapper.save(company);
            //提交事务
            sqlSession.commit();
        } catch (Exception e) {
            //事务回滚
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    /*
    删除
     */
    @Override
    public void delete(Company company) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession对象
            sqlSession = MysqlUtils.getSqlSession();
            //获取动态代理对象
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            mapper.delete(company);
            //提交事务
            sqlSession.commit();
        } catch (Exception e) {
            //事务回滚
            sqlSession.rollback();
            throw new RuntimeException(e);
        } finally {
            //释放资源
            sqlSession.close();
        }
    }

    /*
    更新
     */
    @Override
    public void update(Company company) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession对象
            sqlSession = MysqlUtils.getSqlSession();
            //获取动态代理对象
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            mapper.update(company);
            //提交事务
            sqlSession.commit();
        } catch (Exception e) {
            //事务回滚
            sqlSession.rollback();
            throw new RuntimeException(e);
        } finally {
            //释放资源
            sqlSession.close();
        }
    }

    /*
    通过id查询
     */
    @Override
    public Company findById(String id) {
        SqlSession sqlSession = null;
        Company company = null;
        try {
            //获取sqlSession对象
            sqlSession = MysqlUtils.getSqlSession();
            //获取动态代理对象
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            company = mapper.findById(id);
            //提交事务
            sqlSession.commit();
            return company;
        } catch (Exception e) {
            //事务回滚
            sqlSession.rollback();
            throw new RuntimeException(e);
        } finally {
            //释放资源
            sqlSession.close();
        }
    }

    /*
    查询所有
     */
    @Override
    public List<Company> findAll() {
        SqlSession sqlSession = null;
        List<Company> all = null;
        try {
            //获取sqlSession对象
            sqlSession = MysqlUtils.getSqlSession();
            //获取动态代理对象
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            all = mapper.findAll();
            //提交事务
            sqlSession.commit();
            return all;
        } catch (Exception e) {
            //事务回滚
            sqlSession.rollback();
            throw new RuntimeException(e);
        } finally {
            //释放资源
            sqlSession.close();
        }
    }

    /*
    分页
     */
    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession = null;
        PageInfo pageInfo = null;

        try {
            //获取sqlSession对象
            sqlSession = MysqlUtils.getSqlSession();
            //获取动态代理对象
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            //分页拦截
            PageHelper.startPage(page, size);
            List<Company> list = mapper.findAll();
            pageInfo = new PageInfo(list);
            //提交事务
            sqlSession.commit();
            return pageInfo;
        } catch (Exception e) {
            //事务回滚
            sqlSession.rollback();
            throw new RuntimeException(e);
        } finally {
            //释放资源
            sqlSession.close();
        }
    }
}